<template>
  <div>
    <el-container>
      <el-aside :style="{ width: isCollapse ? 'auto' : '200px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <el-container class="container">
        <el-header>
          <i
            class="el-collapse el-icon-s-fold"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          ></i>
          <div class="user-info">
            <el-avatar
              shape="square"
              size="small"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            ></el-avatar>
            <el-dropdown trigger="click">
              <span class="el-dropdown-link">
                用户的名<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>用户信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="handleLogout"
                  >注销</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>
          Copyright © 2021 - 2021 9I Co. All Rights Reserved. 就爱科技 版权所有
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>真的要注销当前用户吗？请确保当前工作已经保存^_^</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmLogout">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import routes from "../router/routes";
import Navbar from "../components/Navbar";
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollapse: false,
      dialogVisible: false,
    };
  },
  methods: {
    resolve(arr, parentPath = "") {
      let newArr = [];
      //第一步遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route);

        // 当上级路径存在，并且最后一位不为/，则后面添加 /
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;

        // 总是给加上父级路径
        obj.path = parentPath + obj.path;
        // 第二步 判断有没有children属性 如有，那么需要再处理，如果没有则直接push到一个新数组中
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        // 如果当前路由元信息有hidden信息，那么就将其子节点提升上来
        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });

      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleLogout() {
      console.log(17173);
      this.dialogVisible = true;
    },
    handleConfirmLogout() {
      this.dialogVisible = false;
      localStorage.removeItem("accessToken");
      localStorage.removeItem("refreshToken");
      this.$router.push("/login");
    },
  },
  computed: {
    menus() {
      let arr = this.resolve(routes);

      return arr;
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.container {
  height: 100vh;
}
/* .el-aside {
  width: 200px !important;
} */
/* .el-main {
  min-height: calc(100vh - 120px);
} */
.el-footer {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
.el-collapse {
  font-size: 2rem;
}
.collapseTransform {
  transform: rotate(-90deg);
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.user-info {
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-info .el-avatar{
  margin-right: 10px;
}
</style>